/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cn.ekuma.impexp.engine.csv;

import cn.ekuma.impexp.ImpExpEngine;
import cn.ekuma.impexp.ImpExpManager;
import cn.ekuma.impexp.model.AbstractImpExpTableDefinition;
import cn.ekuma.impexp.model.ImpExpRowDefinition;
import com.csvreader.CsvReader;
import com.csvreader.CsvWriter;
import com.openbravo.format.Formats;

import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.nio.charset.Charset;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Administrator
 */
public class CSVEngine implements ImpExpEngine{
    public final static String PROPERTY_prefix="CSV";
    
   public static String PROPERTY_TextQualifier="TextQualifier";
   public static String PROPERTY_Delimiter="Delimiter";
   public static String PROPERTY_RecordDelimiter="RecordDelimiter";
   public static String PROPERTY_CaseSensitive="CaseSensitive";
   public static String PROPERTY_TrimWhitespace="TrimWhitespace";
   public static String PROPERTY_UseTextQualifier="UseTextQualifier";
   public static String PROPERTY_EscapeMode="EscapeMode";
   public static String PROPERTY_CaptureRawRecord="CaptureRawRecord";
   public static String PROPERTY_SkipEmptyRecords="SkipEmptyRecords";


   ImpExpManager manager;

   public void init(ImpExpManager manager) {
        this.manager=manager;
    }


    @Override
    public void exportTable(AbstractImpExpTableDefinition aTable,String fileName) {
    	try {
    		if(fileName==null)
                fileName =manager.getDirStory()+aTable.getBaseTable().getTableName() + ".txt";
            Writer outputStream =new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName), "utf8"));
            CsvWriter csvWriter = new CsvWriter(outputStream,',');
            int rowCount=aTable.getRowCount();
            Formats[] fieldFormats=aTable.getBaseTable().getFieldformat();
            int[] fieldIndex=aTable.getListFieldIndexs();
            ImpExpRowDefinition row;
            for(int j=0;j<rowCount;j++){
            	row=aTable.getRow(j);
            	Object[] data=row.getDatas();
            	String[] ret=new String[data.length];
            	for(int i=0;i<data.length;i++){
            		ret[i] = (String) data[i];
             }
             csvWriter.writeRecord(ret);
            }
            csvWriter.close();   
        } catch (IOException ex) {
        	  ex.printStackTrace();
                Logger.getLogger(CSVEngine.class.getName()).log(Level.SEVERE, null, ex);
         }
    }
    
    
    public void importTable(AbstractImpExpTableDefinition aTable,String fileName) {
    	try {
    		if(fileName==null)
                fileName = manager.getDirStory()+aTable.getBaseTable().getTableName() + ".txt";
            CsvReader csvReader = new CsvReader(fileName,',',Charset.forName("utf8"));
            while(csvReader.readRecord()){
                aTable.addRow(new ImpExpRowDefinition(csvReader.getValues()));
            }
            csvReader.close();
        }catch(FileNotFoundException ex){
        	System.out.println("import fileName nofound:"+fileName);
        }catch (IOException ex) {
        	ex.printStackTrace();
            Logger.getLogger(CSVEngine.class.getName()).log(Level.SEVERE, null, ex);
        } 
    }


}
